/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.unit.flash;
import flash.display.Sprite;
import hapi.flash.util.AssetsUtil;
import haxe.PosInfos;
import haxe.unit.TestCase;

class AssetsTestCase<T:Sprite> extends TestCase {
	
	function assertAssetStructure(expected:T, actual:Sprite, ?c:PosInfos) {
		try {
			checkAssetStructure(expected, actual);
		}catch (e:Dynamic) {
			throwFailure("error", c);
		}
	}
	
	function assertDisplayObjectInLibraryStructure(expected:T, linkage:String, ?c:PosInfos) {
		try {
			var actual = AssetsUtil.create(linkage);
			checkAssetStructure(expected, actual);
		}catch (e:Dynamic) {
			throwFailure(e, c);
		}
	}
	
	function checkAssetStructure(expected:T, actual:Sprite) {
		var f = Reflect.fields(expected);
		var result:StringBuf = new StringBuf();
		var className = Type.getClassName( Type.getClass(expected) );
		for ( f in fields ) {
			checkField(actual, f, result, className);
		}
	}
	
	function checkChildren(expected:T, actual:Sprite) {
		
	}
	
	inline function checkField(actual:Sprite, childName:String, spritePath:String, result:StringBuf) {
		var child = ctual.getChildByName(childName);
		if ( child == null ) {
			result.add("field " + childName + " does not exist in sprite "+spritePath);
		}else if ( Std.is(child, Sprite)) {
			checkChildren(Type
		}
	}
	
	function throwFailure( message:String, c:PosInfos ):Void {
		currentTest.done = true;
		currentTest.success = false;
		currentTest.error = printOnFail;
		currentTest.posInfos = c;
		throw currentTest;
	}
}